import React, {Component} from 'react'

import {
     StyleSheet,
  View,
  Text,
  Image,
  Alert,
  ToastAndroid,
  ActivityIndicator,
  Dimensions,
  TouchableHighlight
} from 'react-native';

const { width, height } = Dimensions.get('window');

const onewidth=(width-55)/4

var styles=StyleSheet.create({
   icon:{
     width:30,
     height:30
   },
   topimg:{
       width:'100%',
       height:0,
       paddingTop:'50%',
       marginTop:0
   },
   imgicon:{
       width:50,
       height:50,
   },
   singui:{
       marginTop:20,
       padding:15,
       backgroundColor:'white',
       width:width,
       justifyContent:'space-around',
       alignItems:'center',
       flexWrap:'wrap',
       flexDirection:'row'
   },
   one:{
     flexDirection:'column',
     justifyContent:'space-around',
     alignItems:'center',
     width:onewidth,
     marginLeft:5,
   },
   topshow:{
     width:width,
     height:120,
     backgroundColor:'white',
     flexDirection:'row',
     justifyContent:'space-around',
     flexWrap:'wrap'
   },
   topshowlist:{
     width:width/3,
     height:60,
     flexDirection:'column',
     borderBottomWidth:1,
     borderColor:'#ddd',
     paddingBottom:2,
     justifyContent:'space-around',
     flexWrap:'wrap',
     alignItems:'center'
   }
})


export default class Money extends Component {
    static navigationOptions = {
    title: '营业状况',
    headerTitleStyle: {
      alignSelf:'center'
    },
    headerStyle:{
        height:45
      },
      tabBarLabel: '营业状况',
    tabBarPosition:'bottom',
    tabBarIcon: () => (
      <Image
        source={require('../img/activity.png')}
        style={styles.icon}
      />
    )
  };

  state={
    payAmount:0,
    successOrderNum:0,
    averPrice:0,
    allAmount:0,
    allOrderNum:0,
    abnormalOrderNum:0,
    url:require('../img/3.png')
  }

    getsmalldate=function(){
      var date = new Date();
    this.year = date.getFullYear();
    this.month = date.getMonth() + 1;
    this.date = date.getDate();
    this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];
    var currentTime =this.year + "-" + this.month + "-" + this.date;
    return currentTime
        }


   getdate =function(){
          var date = new Date();
          this.year = date.getFullYear();
          this.month = date.getMonth() + 1;
          this.date = date.getDate();
          this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];
          this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
          this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
          this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
          var currentTime =this.year + "-" + this.month + "-" + this.date + " " + this.hour + ":" + this.minute + ":" + this.second;
          return currentTime
        }

 async componentWillMount(){

    try {

            let response = await fetch(global.mainurl+"bossHelper/getReportData?sellerid="+global.sellerid+"&userid="+global.userdata.id+"&startDate="+this.getsmalldate()+' 00:00:00'+"&endDate="+this.getdate()+"&token="+global.token);
            let advert= await fetch(global.mainurl+"bossHelper/getAdvert?sellerid="+global.sellerid+"&userid="+global.userdata.id+"&token="+global.token);
            var data=await response.json()
            var addata=await advert.json()
            console.log(addata)
            if(data.code==1){
                        this.setState({
                            payAmount:data.data.payAmount?parseFloat(res.data.payAmount).toFixed(2):0,
                            successOrderNum:data.data.successOrderNum || 0,
                            averPrice:data.data.averPrice?parseFloat(res.data.averPrice).toFixed(2):0,
                            allAmount:data.data.allAmount?parseFloat(res.data.allAmount).toFixed(2):0,
                            allOrderNum:data.data.allOrderNum || 0,
                            abnormalOrderNum:data.data.abnormalOrderNum || 0
                        })
                    }
            else{
								
						}

            if(addata.code==1){
                this.setState({
                  url:{uri:"http://www.dadu999.com"+addata.data[0].mainpic}
                })
            }
            else{

            }
         
            } 
    catch(error) {
      console.log(error)
            ToastAndroid.show('网络出错', ToastAndroid.SHORT);
            }
  }

  render() {
    return (
      <View>
        <Image source={this.state.url}
        resizeMode='stretch'
        style={styles.topimg}
        />
        <View style={styles.topshow}>
        <View style={styles.topshowlist}>
          <Text>
          {this.state.payAmount}
          </Text>
          <Text>
          实时收入
          </Text>
        </View>

        <View style={styles.topshowlist}>
          <Text>
          {this.state.successOrderNum}
          </Text>
          <Text>
          支付单数
          </Text>
        </View>

        <View style={styles.topshowlist}>
          <Text>
          {this.state.averPrice}
          </Text>
          <Text>
          平均单价
          </Text>
        </View>

        <View style={styles.topshowlist}>
          <Text>
          {this.state.allAmount}
          </Text>
          <Text>
          营业额
          </Text>
        </View>

        <View style={styles.topshowlist}>
          <Text>
          {this.state.allOrderNum}
          </Text>
          <Text>
          订单总数
          </Text>
        </View>

        <View style={styles.topshowlist}>
          <Text>
          {this.state.abnormalOrderNum}
          </Text>
          <Text>
          异常单数
          </Text>
        </View>
        </View>
        <View style={styles.singui}>

        <TouchableHighlight style={styles.one}
        onPress={ ()=>{0
          this.props.navigation.navigate('Foodsetwarp')
        }}
        >
        <View>
        <Image source={require('../img/activity.png')}
        style={styles.imgicon}
        >
        </Image>
        <Text>
        菜品设置
        </Text>
        </View>
        </TouchableHighlight>

        <TouchableHighlight style={styles.one}
         onPress={ ()=>{0
          this.props.navigation.navigate('Chartwarp')
        }}
        >
        <View>
        <Image source={require('../img/editor.png')}
        style={styles.imgicon}
        >
        </Image>
        <Text style={{textAlign:'center'}}>
        报表
        </Text>
        </View>
        </TouchableHighlight>

        <TouchableHighlight style={styles.one}
        onPress={ ()=>{0
          this.props.navigation.navigate('Employee')
        }}
        >
        <View>
        <Image source={require('../img/people.png')}
        style={styles.imgicon}
        >
        </Image>
        <Text>
        员工管理
        </Text>
        </View>
        </TouchableHighlight>

        <TouchableHighlight style={styles.one}
        onPress={()=>{
          Alert.alert("会员功能暂未开通")
        }}
        >
        <View>
        <Image source={require('../img/activity.png')}
        style={styles.imgicon}
        >
        </Image>
        <Text>
        会员中心
        </Text>
        </View>
        </TouchableHighlight>

        </View>
      </View>
    );
  }
}